package com.blazingredfireball.rouleurlog.util
{
	import mx.utils.ObjectUtil;
	
	public class SqlUtils
	{
		
		public static function buildUpdateSQL(table:String, updateObject:Object,exclusions:Array,whereField:String):String
		{
			var statement:String = "";
			var objectInfo:Object = ObjectUtil.getClassInfo(updateObject,exclusions);
			
			//Build fieldStatement
			for(var i:Number = 0;i<objectInfo.properties.length;i++)
			{
				var propertyName:String = objectInfo.properties[i]
				if(propertyName != whereField)
				{
					statement +=  propertyName + " = '" + updateObject[propertyName] + "'"
					if(i!=objectInfo.properties.length-1) 
					{
						statement += ", ";
					}
				}
			}
			
			var result:String = "UPDATE " + table + " SET " + statement + "WHERE id = " + updateObject[whereField];

			return result;
		}
	
		public static function buildCreateSQL(table:String, updateObject:Object, exclusions:Array = null):String
		{
			var fieldStatement:String = "";
			var valuesStatement:String = "'";
			var objectInfo:Object = ObjectUtil.getClassInfo(updateObject,exclusions);
			
			//Build fieldStatement
			for(var i:Number = 0;i<objectInfo.properties.length;i++)
			{
				fieldStatement += objectInfo.properties[i];
				valuesStatement += updateObject[objectInfo.properties[i]] + "'";
				if(i!=objectInfo.properties.length-1) 
				{
					fieldStatement += ", ";
					valuesStatement += ", '";
				}
			}
			
			var result:String = "INSERT INTO " + table + " (" + fieldStatement + ") VALUES (" + valuesStatement + ")";

			return result;
		}
	}	
}